package com.moody.hibernate.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.ModelHis;
import com.moody.hibernate.domain.QuaValue;

/**
 	* A data access object (DAO) providing persistence and search support for QuaValue entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see com.moody.hibernate.domain.QuaValue
  * @author MyEclipse Persistence Tools 
 */

public class QuaValueDAO extends HibernateDaoSupport  {
	     private static final Logger log = LoggerFactory.getLogger(QuaValueDAO.class);
		//property constants
	public static final String ITEM_SELECT = "itemSelect";
	public static final String ITEM_VALUE = "itemValue";
	public static final String ITEM_WEIGHT = "itemWeight";



	protected void initDao() {
		//do nothing
	}
    
    public void save(QuaValue transientInstance) {
        log.debug("saving QuaValue instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(QuaValue persistentInstance) {
        log.debug("deleting QuaValue instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public QuaValue findById( com.moody.hibernate.domain.QuaValueId id) {
        log.debug("getting QuaValue instance with id: " + id);
        try {
            QuaValue instance = (QuaValue) getHibernateTemplate()
                    .get("com.moody.hibernate.domain.QuaValue", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(QuaValue instance) {
        log.debug("finding QuaValue instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding QuaValue instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from QuaValue as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByItemSelect(Object itemSelect
	) {
		return findByProperty(ITEM_SELECT, itemSelect
		);
	}
	
	public List findByItemValue(Object itemValue
	) {
		return findByProperty(ITEM_VALUE, itemValue
		);
	}
	
	public List findByItemWeight(Object itemWeight
	) {
		return findByProperty(ITEM_WEIGHT, itemWeight
		);
	}
	

	public List findAll() {
		log.debug("finding all QuaValue instances");
		try {
			String queryString = "from QuaValue";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public QuaValue merge(QuaValue detachedInstance) {
        log.debug("merging QuaValue instance");
        try {
            QuaValue result = (QuaValue) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(QuaValue instance) {
        log.debug("attaching dirty QuaValue instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(QuaValue instance) {
        log.debug("attaching clean QuaValue instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static QuaValueDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (QuaValueDAO) ctx.getBean("QuaValueDAO");
	}

	public QuaValue findQuaValueBySerialNoModelIdAndItemNo(String serialNo, String modelId, String itemNo) {
		String queryString = "from QuaValue as qv where qv.id.serialNo = ? and qv.id.modelId = ? and qv.id.itemNo= ?";
		List<QuaValue> list = getHibernateTemplate().find(queryString, serialNo, modelId, itemNo);
		
		if(list!=null && list.size()>0)
			return list.get(0);
		else
			return null;	
	}

	public void insertQuaValueList(List<QuaValue> list) {
		try{
			getHibernateTemplate().saveOrUpdateAll(list);
		}catch (Exception e){
			log.error("insert error", e);
		}
	}

	public List<QuaValue> findQuaValueBySerialNoAndModelId(String serialNo,String modelId) {
		String queryString = "from QuaValue as q, ModelInfo as m where q.id.itemNo = m.itemNo and q.itemValue = m.itemValue and q.id.serialNo = ? and q.id.modelId = ? order by m.serialNo";
		List list = getHibernateTemplate().find(queryString, serialNo, modelId);
		List<QuaValue> resultList = new ArrayList<QuaValue>();
		for(Object o :list){
			Object[] temp = (Object[]) o;
			resultList.add((QuaValue)temp[0]);
		}
		
		return resultList;
	}
}